package com.ruoyi.train.service.impl;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.layout.element.Image;

import com.ruoyi.common.domain.TrainingInstitutionInfomation;
import com.ruoyi.common.service.ITrainingInstitutionInfomationService;

import com.ruoyi.config.pdf.method.PdfCreate;
import com.ruoyi.config.pdf.vo.PdfReqVo;

import com.ruoyi.train.domain.*;
import com.ruoyi.train.domain.Vo.CertApplicationStudentVo;
import com.ruoyi.train.domain.Vo.CertApplicationVo;
import com.ruoyi.train.service.*;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.train.mapper.CertApplicationMapper;

/**
 * 办证申请Service业务层处理
 * 
 * @author wusihao
 * @date 2023-04-18
 */
@Service
public class CertApplicationServiceImpl extends ServiceImpl<CertApplicationMapper, CertApplication> implements ICertApplicationService
{
    @Autowired
    private CertApplicationMapper certApplicationMapper;

    @Autowired
    private ICertApplicationService certApplicationService;

    @Autowired
    private ICertApplicationStudentService certApplicationStudentService;

    @Autowired
    private IExamPassStudentService examPassStudentService;

    @Autowired
    private IExamPassCertificateService examPassCertificateService;

    @Autowired
    private ITrainingInstitutionInfomationService trainingInstitutionInfomationService;

    @Autowired
    private IExamApplicationService examApplicationService;

    @Autowired
    private IExamApplicationStudentService examApplicationStudentService;

    @Autowired
    private IStudentInformationService studentInformationService;

    @Autowired
    private IStudentProcessRecordService studentProcessRecordService;

    @Autowired
    private ITrainPlanService trainPlanService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询办证申请
     * 
     * @param
     * @return 办证申请
     */
    @Override
    public CertApplicationVo selectCertApplicationById(Long certApplicationId)
    {
        CertApplication certApplication = certApplicationService.getById(certApplicationId);
        CertApplicationVo certApplicationVo = new CertApplicationVo();
        BeanUtils.copyProperties(certApplication, certApplicationVo);
        // 通过办证申请表id查询学生id
        Long certApplicationVoId = certApplicationVo.getId();
        // 通过办证申请表id查询办证申请学生信息
        QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
        certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
        List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
        // 通过学生id找到考试申请学生/合格证明学生表，进而找到考试/申请合格证明表
        Long studentId = certApplicationStudentList.get(0).getStudentId();
        // 通过学生id得到培训计划申请
        StudentInformation studentInformationServiceById = studentInformationService.getById(studentId);
        QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
        studentProcessRecordQueryWrapper.eq("serial_number", studentInformationServiceById.getSerialNumber());
        StudentProcessRecord studentProcessRecordServiceOne = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
        Long trainPlanId = studentProcessRecordServiceOne.getTrainPlanId();
        TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
        certApplicationVo.setTrainplanApplication(trainPlanServiceById.getTrainplanApplication());
        // 考试申请表
        QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
        examApplicationStudentQueryWrapper.eq("student_id", studentId);
        List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
        Long examApplicationId = examApplicationStudents.get(0).getExamApplicationId();
        ExamApplication examApplicationServiceById = examApplicationService.getById(examApplicationId);
        certApplicationVo.setExamApplicationPdf(examApplicationServiceById.getExamApplicationPdf());
        QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
        examPassStudentQueryWrapper.eq("student_id", studentId);
        List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
        Long examPassId = examPassStudents.get(0).getExamPassId();
        // 通过考试合格证明表id找到考试合格证明表
        ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
        certApplicationVo.setExamCertificatePdf(examPassCertificateServiceById.getExamCertificatePdf());
        // 得到考试期次
        certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());
        // 通过当前登录的管理员id得到申报单位公章


        return certApplicationVo;
    }

    @Override
    public CertApplicationVo getCertApplicationById(Long certApplicationId) {
        CertApplication certApplication = certApplicationService.getById(certApplicationId);
        CertApplicationVo certApplicationVo = new CertApplicationVo();
        BeanUtils.copyProperties(certApplication, certApplicationVo);
        // 通过办证申请表id查询学生id
        Long certApplicationVoId = certApplicationVo.getId();
        // 通过办证申请表id查询办证申请学生信息
        QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
        certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
        List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
        Long studentId = certApplicationStudentList.get(0).getStudentId();
        // 通过学生id得到培训计划申请
        StudentInformation studentInformationServiceById = studentInformationService.getById(studentId);
        QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
        studentProcessRecordQueryWrapper.eq("serial_number", studentInformationServiceById.getSerialNumber());
        StudentProcessRecord studentProcessRecordServiceOne = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
        Long trainPlanId = studentProcessRecordServiceOne.getTrainPlanId();
        TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
        certApplicationVo.setTrainplanApplication(trainPlanServiceById.getTrainplanApplication());
        // 通过学生id找到考试申请学生表，进而找到考试申请表
        QueryWrapper<ExamApplicationStudent> examApplicationStudentQueryWrapper = new QueryWrapper<>();
        examApplicationStudentQueryWrapper.eq("student_id", studentId);
        List<ExamApplicationStudent> examApplicationStudents = examApplicationStudentService.list(examApplicationStudentQueryWrapper);
        Long examApplicationId = examApplicationStudents.get(0).getExamApplicationId();
        ExamApplication examApplicationServiceById = examApplicationService.getById(examApplicationId);
        certApplicationVo.setExamApplicationPdf(examApplicationServiceById.getExamApplicationPdf());

        // 通过学生id找到考试合格证明学生表，进而找到考试合格证明表
        QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
        examPassStudentQueryWrapper.eq("student_id", studentId);
        List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
        Long examPassId = examPassStudents.get(0).getExamPassId();
        // 通过考试合格证明表id找到考试合格证明表
        ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
        certApplicationVo.setExamCertificatePdf(examPassCertificateServiceById.getExamCertificatePdf());
        // 得到考试期次
        certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());
        // 通过当前登录的管理员id得到申报单位公章
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<TrainingInstitutionInfomation> trainingInstitutionInfomationQueryWrapper = new QueryWrapper<>();
        trainingInstitutionInfomationQueryWrapper.eq("create_id", userId);
        TrainingInstitutionInfomation trainingInstitutionInfomationServiceOne = trainingInstitutionInfomationService.getOne(trainingInstitutionInfomationQueryWrapper);
        certApplicationVo.setApplicationUnitSheet(trainingInstitutionInfomationServiceOne.getApplicationUnitSheet());
        certApplicationVo.setUnitHeader(trainingInstitutionInfomationServiceOne.getUnitHeader());
        certApplicationVo.setUnitManager(trainingInstitutionInfomationServiceOne.getUnitManager());
        return certApplicationVo;
    }

    /**
     * 查询办证申请列表
     * 
     * @param
     * @return 办证申请
     */
    @Override
    public List<CertApplicationVo> selectCertApplicationList(CertApplication certApplication1)
    {
        // 返回最终答案集合
        List<CertApplicationVo> certApplicationVoList = new ArrayList<>();
        QueryWrapper<CertApplication> certApplicationQueryWrapper = new QueryWrapper<>();
        certApplicationQueryWrapper.eq("is_deleted", 0);
        certApplicationQueryWrapper.eq("qualified_type", 107);
        certApplicationQueryWrapper.eq("is_pass", 0);
        certApplicationQueryWrapper.eq("create_id", SecurityUtils.getUserId());
        List<CertApplication> certApplicationList = certApplicationService.list(certApplicationQueryWrapper);
        for (CertApplication certApplication : certApplicationList) {
            if (certApplication == null) continue;
            // 存储答案对象
            CertApplicationVo certApplicationVo = new CertApplicationVo();
            BeanUtils.copyProperties(certApplication, certApplicationVo);
            // 通过办证申请表id查询学生id
            Long certApplicationVoId = certApplicationVo.getId();
            // 通过办证申请表id查询办证申请学生信息
            QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
            certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
            List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
            // 通过学生id找到考试合格证明学生表，进而找到考试合格证明表
            Long studentId = certApplicationStudentList.get(0).getStudentId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("student_id", studentId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            Long examPassId = examPassStudents.get(0).getExamPassId();
            // 通过考试合格证明表id找到考试合格证明表
            ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
            // 得到考试期次
            certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());

            // 存入最终答案中
            certApplicationVoList.add(certApplicationVo);
        }
        if (certApplicationList == null || certApplicationList.size() == 0) return new ArrayList<>();
        return certApplicationVoList;
    }

    // 查询特作审批一列表
    @Override
    public List<CertApplicationVo> selecttezhongCertApplicationOneList(CertApplication certApplication1) {
        // 返回最终答案集合
        List<CertApplicationVo> certApplicationVoList = new ArrayList<>();
        QueryWrapper<CertApplication> certApplicationQueryWrapper = new QueryWrapper<>();
        certApplicationQueryWrapper.eq("is_deleted", 0);
        certApplicationQueryWrapper.eq("approval_process_sign", 1);
        certApplicationQueryWrapper.eq("qualified_type", 107);
        List<CertApplication> certApplicationList = certApplicationService.list(certApplicationQueryWrapper);
        for (CertApplication certApplication : certApplicationList) {
            // 存储答案对象
            CertApplicationVo certApplicationVo = new CertApplicationVo();
            BeanUtils.copyProperties(certApplication, certApplicationVo);
            // 通过办证申请表id查询学生id
            Long certApplicationVoId = certApplicationVo.getId();
            // 通过办证申请表id查询办证申请学生信息
            QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
            certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
            List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
            // 通过学生id找到考试合格证明学生表，进而找到考试合格证明表
            Long studentId = certApplicationStudentList.get(0).getStudentId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("student_id", studentId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            Long examPassId = examPassStudents.get(0).getExamPassId();
            // 通过考试合格证明表id找到考试合格证明表
            ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
            // 得到考试期次
            certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());


            // 存入最终答案中
            certApplicationVoList.add(certApplicationVo);
        }

        return certApplicationVoList;
    }

    @Override
    public List<CertApplicationVo> selecttezhongCertApplicationgaizhangList(CertApplication certApplication1) {
        // 返回最终答案集合
        List<CertApplicationVo> certApplicationVoList = new ArrayList<>();
        QueryWrapper<CertApplication> certApplicationQueryWrapper = new QueryWrapper<>();
        certApplicationQueryWrapper.eq("is_deleted", 0);
        certApplicationQueryWrapper.eq("approval_process_sign", 3);
        certApplicationQueryWrapper.eq("qualified_type", 107);
        List<CertApplication> certApplicationList = certApplicationService.list(certApplicationQueryWrapper);
        for (CertApplication certApplication : certApplicationList) {
            // 存储答案对象
            CertApplicationVo certApplicationVo = new CertApplicationVo();
            BeanUtils.copyProperties(certApplication, certApplicationVo);
            // 通过办证申请表id查询学生id
            Long certApplicationVoId = certApplicationVo.getId();
            // 通过办证申请表id查询办证申请学生信息
            QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
            certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
            List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
            // 通过学生id找到考试合格证明学生表，进而找到考试合格证明表
            Long studentId = certApplicationStudentList.get(0).getStudentId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("student_id", studentId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            Long examPassId = examPassStudents.get(0).getExamPassId();
            // 通过考试合格证明表id找到考试合格证明表
            ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
            // 得到考试期次
            certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());


            // 存入最终答案中
            certApplicationVoList.add(certApplicationVo);
        }
        if (certApplicationList.size() == 0 || certApplicationList == null) return new ArrayList<>();
        return certApplicationVoList;
    }


    /**
     * 新增办证申请
     * 
     * @param certApplication 办证申请
     * @return 结果
     */
    @Override
    public int insertCertApplication(CertApplication certApplication)
    {
        certApplication.setCreateTime(DateUtils.getNowDate());
        return certApplicationMapper.insertCertApplication(certApplication);
    }

    /**
     * 修改办证申请
     * 
     * @param certApplication 办证申请
     * @return 结果
     */
    @Override
    public int updateCertApplication(CertApplication certApplication)
    {
        certApplication.setUpdateTime(DateUtils.getNowDate());
        return certApplicationMapper.updateCertApplication(certApplication);
    }

    /**
     * 批量删除办证申请
     * 
     * @param ids 需要删除的办证申请主键
     * @return 结果
     */
    @Override
    public int deleteCertApplicationByIds(Long[] ids)
    {
        return certApplicationMapper.deleteCertApplicationByIds(ids);
    }

    /**
     * 删除办证申请信息
     * 
     * @param id 办证申请主键
     * @return 结果
     */
    @Override
    public int deleteCertApplicationById(Long id)
    {
        return certApplicationMapper.deleteCertApplicationById(id);
    }


    // 培训机构提交办证申请表
    @Override
    public int peixunjigousubmittezhongCertApplication(Long certApplicationId) {
        CertApplication certApplicationServiceById = certApplicationService.getById(certApplicationId);
        certApplicationServiceById.setUpdateId(SecurityUtils.getUserId());
        certApplicationServiceById.setUpdateTime(DateUtils.getNowDate());
        certApplicationServiceById.setApprovalProcessSign(1L);
        return certApplicationService.updateCertApplication(certApplicationServiceById);
    }

    // 办证申请表特作审批一
    @Override
    public int tezhongOneApprovalSubmit(CertApplication certCertificate) {
        certCertificate.setUpdateTime(DateUtils.getNowDate());
        certCertificate.setUpdateId(SecurityUtils.getUserId());
        certCertificate.setAuditId(SecurityUtils.getUserId());
            certCertificate.setApprovalProcessSign(2L);

        return certApplicationService.updateCertApplication(certCertificate);
    }

    @Override
    public List<CertApplicationVo> selecttezhongCertApplicationTwoList(CertApplication certApplication1) {
        // 返回最终答案集合
        List<CertApplicationVo> certApplicationVoList = new ArrayList<>();
        QueryWrapper<CertApplication> certApplicationQueryWrapper = new QueryWrapper<>();
        certApplicationQueryWrapper.eq("is_deleted", 0);
        certApplicationQueryWrapper.eq("approval_process_sign", 2);
        certApplicationQueryWrapper.eq("qualified_type", 107);
        List<CertApplication> certApplicationList = certApplicationService.list(certApplicationQueryWrapper);
        for (CertApplication certApplication : certApplicationList) {
            // 存储答案对象
            CertApplicationVo certApplicationVo = new CertApplicationVo();
            BeanUtils.copyProperties(certApplication, certApplicationVo);
            // 通过办证申请表id查询学生id
            Long certApplicationVoId = certApplicationVo.getId();
            // 通过办证申请表id查询办证申请学生信息
            QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
            certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
            List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
            // 通过学生id找到考试合格证明学生表，进而找到考试合格证明表
            Long studentId = certApplicationStudentList.get(0).getStudentId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("student_id", studentId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            Long examPassId = examPassStudents.get(0).getExamPassId();
            // 通过考试合格证明表id找到考试合格证明表
            ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
            // 得到考试期次
            certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());


            // 存入最终答案中
            certApplicationVoList.add(certApplicationVo);
        }

        return certApplicationVoList;
    }

    // 合格证明表特作审批二
    @Override
    public int tezhongTwoApprovalSubmit(CertApplication certCertificate) {
        certCertificate.setUpdateTime(DateUtils.getNowDate());
        certCertificate.setUpdateId(SecurityUtils.getUserId());
        certCertificate.setApprovalId(SecurityUtils.getUserId());
        certCertificate.setApprovalProcessSign(3L);

        return certApplicationService.updateCertApplication(certCertificate);
    }

    @Override
    public int tezhonggaizhangCertApplication(CertApplication certApplication) {
        certApplication.setGaizhangId(SecurityUtils.getUserId());
        // 通过办证申请表信息得到办证申请表需要生产的pdf
        certApplicationService.updateCertApplication(certApplication);
        CertApplicationVo certApplicationVo = getCertInformationByCertInformation(certApplication.getId());
        // 通过办证申请表id得到学生信息
        List<CertApplicationStudentVo> certApplicationStudentVoList = getCertStudentInformationByCertApplicationId(certApplication.getId());
        certApplicationVo.setCertApplicationStudentVoList(certApplicationStudentVoList);
        // 生产办证申请表pdf
        String pdfUrl = getCertPdfByCertInformation(certApplicationVo);
        ExamApplicationServiceImpl applicationService = new ExamApplicationServiceImpl();
        String pdfVirtualUrlByTrueUrl = applicationService.getPdfVirtualUrlByTrueUrl(pdfUrl);
        certApplication.setCertApplicationPdf(pdfVirtualUrlByTrueUrl);
        certApplication.setUpdateId(SecurityUtils.getUserId());
        certApplication.setUpdateTime(DateUtils.getNowDate());
        certApplication.setIsPass(1);
        certApplication.setApprovalProcessSign(4L);

        return certApplicationService.updateCertApplication(certApplication);
    }

    // TODO 上传服务器地址
    // 生产办证申请表pdf
    private String getCertPdfByCertInformation(CertApplicationVo certApplicationVo) {

        String qualifiedType = certApplicationVo.getQualifiedType();
        Integer template = -1;
        if (qualifiedType.equals("105") || qualifiedType.equals("106")) template = 5;
        else if (qualifiedType.equals("107")) template = 6;

        List<String> oneBasicList = new ArrayList<>();
        if (certApplicationVo.getApplicationUnit() != null) oneBasicList.add(certApplicationVo.getApplicationUnit());
        else oneBasicList.add("");
        if (certApplicationVo.getClassTerm() != null) oneBasicList.add(certApplicationVo.getClassTerm());
        else oneBasicList.add("");

        List<String> twoBasicList = new ArrayList<>();
        twoBasicList.add("");
        twoBasicList.add("");
        twoBasicList.add(String.valueOf(certApplicationVo.getSum()));
        twoBasicList.add(DateUtils.getFormatTime(certApplicationVo.getSendTime().toString()).substring(0, 10));

        List<List<String>> content = new ArrayList<>();
        List<CertApplicationStudentVo> certApplicationStudentVoList = certApplicationVo.getCertApplicationStudentVoList();
        for (CertApplicationStudentVo certApplicationStudentVo : certApplicationStudentVoList) {
            List<String> line = new ArrayList<>();
            if (certApplicationStudentVo.getIdCard() != null) line.add(certApplicationStudentVo.getIdCard());
            else line.add("");
            if (certApplicationStudentVo.getName() != null) line.add(certApplicationStudentVo.getName());
            else line.add("");
            if (certApplicationStudentVo.getWorkplace() != null) line.add(certApplicationStudentVo.getWorkplace());
            else line.add("");
            if (certApplicationStudentVo.getPhone() != null) line.add(certApplicationStudentVo.getPhone());
            else line.add("");
            String qualified = certApplicationStudentVo.getQualified();
            if (qualified.equals("105") || qualified.equals("106")) {
                String workKind = certApplicationStudentVo.getWorkKind();
                if (workKind != null) {
                    SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(qualified));
                    String dictLabel = dictData.getDictLabel();
                    SysDictData dictData1 = dictDataService.selectDictDataById(Long.parseLong(workKind));
                    String dictLabel1 = dictData1.getDictLabel();
                    line.add(dictLabel + dictLabel1);
                } else line.add("");
            } else if (qualified.equals("107")) {
                String operationType = certApplicationStudentVo.getOperationType();
                if (operationType != null) {
                    SysDictData dictData = dictDataService.selectDictDataById(Long.parseLong(operationType));
                    String dictLabel = dictData.getDictLabel();
                    line.add(dictLabel);
                } else line.add("");
            } else line.add("");

            if (certApplicationStudentVo.getExamScore() != null) line.add(certApplicationStudentVo.getExamScore());
            else line.add("");
            if (qualified.equals("107")) {
                if (certApplicationStudentVo.getOperatingScore() != null) {
                    line.add(certApplicationStudentVo.getOperatingScore());
                }
                else line.add("");
            }
            line.add("");

            content.add(line);
        }
        List<String> finalDataList = new ArrayList<>();
        if (certApplicationVo.getSum() != null) finalDataList.add(String.valueOf(certApplicationVo.getSum()));
        else finalDataList.add("");

        List<String> approvalList = new ArrayList<>();
        approvalList.add("");
        approvalList.add("");
        String sendTime = DateUtils.getFormatTime(certApplicationVo.getSendTime().toString());
        approvalList.add("九江市应急管理局\n" + sendTime.substring(0, 4) + "年" + sendTime.substring(5, 7) + "月" + sendTime.substring(8, 10) + "日");

        Map<String, Image> sealMap = new HashMap<>();
        try {
            String applicationUnitSheet = certApplicationVo.getApplicationUnitSheet();
            if (applicationUnitSheet != null) {
//                applicationUnitSheet = "C:/wusihao/uploadPath" + applicationUnitSheet.substring(8);
                applicationUnitSheet = "D:/ruoyi/uploadPath" + applicationUnitSheet.substring(8);
                Image applicationUnitSheetImage = new Image(ImageDataFactory.create(applicationUnitSheet));
                sealMap.put("申报单位", applicationUnitSheetImage);
            }

            String gaizhang = certApplicationVo.getGaizhang();
            if (gaizhang != null) {
                gaizhang = "D:/ruoyi/uploadPath" + gaizhang.substring(8);
//                gaizhang = "C:/wusihao/uploadPath" + gaizhang.substring(8);
                Image gaizhangImage = new Image(ImageDataFactory.create(gaizhang));
                sealMap.put("发证机关：", gaizhangImage);
            }

        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        Map<String, Image> signMap = new HashMap<>();
        try {
            String unitHeader = certApplicationVo.getUnitHeader();
            if (unitHeader != null) {
                unitHeader = "D:/ruoyi/uploadPath" + unitHeader.substring(8);
//                unitHeader = "C:/wusihao/uploadPath" + unitHeader.substring(8);
                Image unitHeaderImage = new Image(ImageDataFactory.create(unitHeader));
                sealMap.put("单位负责人", unitHeaderImage);
            }

            String unitManager = certApplicationVo.getUnitManager();
            if (unitManager != null) {
               unitManager = "D:/ruoyi/uploadPath" + unitManager.substring(8);
//                unitManager = "C:/wusihao/uploadPath" + unitManager.substring(8);
                Image unitManagerImage = new Image(ImageDataFactory.create(unitManager));
                sealMap.put("单位经办人", unitManagerImage);
            }

            String auditSign = certApplicationVo.getAuditSign();
            if (auditSign != null) {
                auditSign = "D:/ruoyi/uploadPath" + auditSign.substring(8);
//                auditSign = "C:/wusihao/uploadPath" + auditSign.substring(8);
                Image auditSignImage = new Image(ImageDataFactory.create(auditSign));
                sealMap.put("审核人", auditSignImage);
            }

            String approvalSign = certApplicationVo.getApprovalSign();
            if (approvalSign != null) {
               approvalSign = "D:/ruoyi/uploadPath" + approvalSign.substring(8);
//                approvalSign = "C:/wusihao/uploadPath" + approvalSign.substring(8);
                Image approvalSignImage = new Image(ImageDataFactory.create(approvalSign));
                sealMap.put("审批人", approvalSignImage);
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        PdfReqVo reqVo = new PdfReqVo();
        reqVo.setTemplate(template);
        reqVo.setOneBasicList(oneBasicList);
        reqVo.setTwoBasicList(twoBasicList);
        reqVo.setTemplate(template);
        reqVo.setContent(content);

        reqVo.setFinalDataList(finalDataList);
        reqVo.setApprovalList(approvalList);
        reqVo.setSignMap(signMap);
        reqVo.setSealMap(sealMap);
        try {
            String pdf = PdfCreate.createPdf(reqVo);
            return pdf;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private List<CertApplicationStudentVo> getCertStudentInformationByCertApplicationId(Long certApplicationId) {
        // 答案集合
        List<CertApplicationStudentVo> certApplicationVoList = new ArrayList<>();
        // 通过办证申请表id的到办证学生信息
        QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
        certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationId);
        List<CertApplicationStudent> certApplicationStudents = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
        for (CertApplicationStudent certApplicationStudent : certApplicationStudents) {
            CertApplicationStudentVo certApplicationStudentVo = new CertApplicationStudentVo();
            // 得到学生id
            Long studentId = certApplicationStudent.getStudentId();
            // 通过学生id得到学生信息表
            QueryWrapper<StudentInformation> studentInformationQueryWrapper = new QueryWrapper<>();
            studentInformationQueryWrapper.eq("is_deleted", 0);
            studentInformationQueryWrapper.eq("id", studentId);
            StudentInformation studentInformation = studentInformationService.getOne(studentInformationQueryWrapper);
            if (studentInformation == null) continue;
            certApplicationStudentVo.setIdCard(studentInformation.getIdCard());
            certApplicationStudentVo.setName(studentInformation.getName());
            certApplicationStudentVo.setWorkplace(studentInformation.getWorkplace());
            certApplicationStudentVo.setPhone(studentInformation.getPhone());

            // 通过学生编号得到学生记录表
            String serialNumber = studentInformation.getSerialNumber();
            QueryWrapper<StudentProcessRecord> studentProcessRecordQueryWrapper = new QueryWrapper<>();
            studentProcessRecordQueryWrapper.eq("exam_application_is_pass", 1);
            studentProcessRecordQueryWrapper.eq("exam_certificate_is_pass", 1);
            studentProcessRecordQueryWrapper.eq("cert_is_pass", 1);
            studentProcessRecordQueryWrapper.eq("serial_number", serialNumber);
            StudentProcessRecord studentProcessRecordServiceOne = studentProcessRecordService.getOne(studentProcessRecordQueryWrapper);
            certApplicationStudentVo.setExamScore(studentProcessRecordServiceOne.getExamScore());
            certApplicationStudentVo.setOperatingScore(studentProcessRecordServiceOne.getOperatingScore());

            // 得到培训计划id
            Long trainPlanId = studentProcessRecordServiceOne.getTrainPlanId();
            TrainPlan trainPlanServiceById = trainPlanService.getById(trainPlanId);
            certApplicationStudentVo.setQualified(trainPlanServiceById.getQualifiedType());
            certApplicationStudentVo.setWorkKind(trainPlanServiceById.getWorkKind());
            certApplicationStudentVo.setOperationType(trainPlanServiceById.getOperationType());
            certApplicationVoList.add(certApplicationStudentVo);
        }
        return certApplicationVoList;
    }

    // 通过办证申请表信息得到办证申请表需要生产的pdf
    private CertApplicationVo getCertInformationByCertInformation(Long certApplicationId) {
        // 返回答案集合
        CertApplicationVo certApplicationVo = new CertApplicationVo();
        CertApplication certApplication = certApplicationService.getById(certApplicationId);
        certApplicationVo.setQualifiedType(certApplication.getQualifiedType());
        // 通过创建id得到申请单位，单位负责人，单位经办人信息
        Long createId = certApplication.getCreateId();
        QueryWrapper<TrainingInstitutionInfomation> trainingInstitutionInfomationQueryWrapper = new QueryWrapper<>();
        trainingInstitutionInfomationQueryWrapper.eq("create_id", createId);
        TrainingInstitutionInfomation trainingInstitutionInfomationServiceOne = trainingInstitutionInfomationService.getOne(trainingInstitutionInfomationQueryWrapper);
        certApplicationVo.setApplicationUnit(trainingInstitutionInfomationServiceOne.getName());
        certApplicationVo.setApplicationUnitSheet(trainingInstitutionInfomationServiceOne.getApplicationUnitSheet());
        certApplicationVo.setUnitManager(trainingInstitutionInfomationServiceOne.getUnitManager());
        certApplicationVo.setUnitHeader(trainingInstitutionInfomationServiceOne.getUnitHeader());

        // 通过办证申请表id得到培训班期次
        Long applicationId = certApplication.getId();
        QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
        certApplicationStudentQueryWrapper.eq("cert_application_id", applicationId);
        List<CertApplicationStudent> certApplicationStudents = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
        certApplicationVo.setSum(Long.parseLong(String.valueOf(certApplicationStudents.size())));
        // 得到学生id
        Long studentId = certApplicationStudents.get(0).getStudentId();
        // 通过学生id得到考试合格证明表
        QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
        examPassStudentQueryWrapper.eq("student_id", studentId);
        List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
        Long examPassId = examPassStudents.get(0).getExamPassId();
        ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
        // 得到培训班其次
        certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());
        certApplicationVo.setSendTime(certApplication.getSendTime());

        // 得到经办人签字图片
        Long auditId = certApplication.getAuditId();
        QueryWrapper<TrainingInstitutionInfomation> trainingInstitutionInfomationQueryWrapper1 = new QueryWrapper<>();
        trainingInstitutionInfomationQueryWrapper1.eq("create_id", auditId);
        TrainingInstitutionInfomation trainingInstitutionInfomationServiceOne1 = trainingInstitutionInfomationService.getOne(trainingInstitutionInfomationQueryWrapper1);
        certApplicationVo.setAuditSign(trainingInstitutionInfomationServiceOne1.getApplicationUnitSheet());

        // 得到负责人签字图片
        Long approvalId = certApplication.getApprovalId();
        QueryWrapper<TrainingInstitutionInfomation> trainingInstitutionInfomationQueryWrapper2 = new QueryWrapper<>();
        trainingInstitutionInfomationQueryWrapper2.eq("create_id", approvalId);
        TrainingInstitutionInfomation trainingInstitutionInfomationServiceOne2 = trainingInstitutionInfomationService.getOne(trainingInstitutionInfomationQueryWrapper2);
        certApplicationVo.setApprovalSign(trainingInstitutionInfomationServiceOne2.getApplicationUnitSheet());

        // 得到盖章图片
        Long gaizhangId = certApplication.getGaizhangId();
        QueryWrapper<TrainingInstitutionInfomation> trainingInstitutionInfomationQueryWrapper3 = new QueryWrapper<>();
        trainingInstitutionInfomationQueryWrapper3.eq("create_id", gaizhangId);
        TrainingInstitutionInfomation trainingInstitutionInfomationServiceOne3 = trainingInstitutionInfomationService.getOne(trainingInstitutionInfomationQueryWrapper3);
        certApplicationVo.setGaizhang(trainingInstitutionInfomationServiceOne3.getApplicationUnitSheet());
        return certApplicationVo;
    }

    @Override
    public List<CertApplicationVo> selectfuzerenCertSubmitApplicationList(CertApplication certApplication1) {
        // 返回最终答案集合
        List<CertApplicationVo> certApplicationVoList = new ArrayList<>();
        QueryWrapper<CertApplication> certApplicationQueryWrapper = new QueryWrapper<>();
        certApplicationQueryWrapper.eq("is_deleted", 0);
        certApplicationQueryWrapper.eq("create_id", SecurityUtils.getUserId());
        certApplicationQueryWrapper.and(temp -> temp.eq("qualified_type", 105).or().eq("qualified_type", 106));
        List<CertApplication> certApplicationList = certApplicationService.list(certApplicationQueryWrapper);

        for (CertApplication certApplication : certApplicationList) {
            if (certApplication == null) continue;
            // 存储答案对象
            CertApplicationVo certApplicationVo = new CertApplicationVo();
            BeanUtils.copyProperties(certApplication, certApplicationVo);
            // 通过办证申请表id查询学生id
            Long certApplicationVoId = certApplicationVo.getId();
            // 通过办证申请表id查询办证申请学生信息
            QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
            certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
            List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
            // 通过学生id找到考试合格证明学生表，进而找到考试合格证明表
            Long studentId = certApplicationStudentList.get(0).getStudentId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("student_id", studentId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            Long examPassId = examPassStudents.get(0).getExamPassId();
            // 通过考试合格证明表id找到考试合格证明表
            ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
            // 得到考试期次
            certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());
            certApplicationVo.setExamCertificatePdf(examPassCertificateServiceById.getExamCertificatePdf());

            // 存入最终答案中
            certApplicationVoList.add(certApplicationVo);
        }

        if (certApplicationList.size() == 0 || certApplicationList == null) return new ArrayList<>();
        return certApplicationVoList;
    }

    // 培训机构提交负责人办证申请
    @Override
    public int peixunjigousubmitfuzerenCertApplication(Long certApplicationId) {
        CertApplication certApplicationServiceById = certApplicationService.getById(certApplicationId);
        certApplicationServiceById.setCreateId(SecurityUtils.getUserId());
        certApplicationServiceById.setCreateTime(DateUtils.getNowDate());
        certApplicationServiceById.setApprovalProcessSign(1L);
        return certApplicationService.updateCertApplication(certApplicationServiceById);
    }

    @Override
    public List<CertApplicationVo> selectfuzerenCertApplicationOneList(CertApplication certApplication1) {
        // 返回最终答案集合
        List<CertApplicationVo> certApplicationVoList = new ArrayList<>();
        QueryWrapper<CertApplication> certApplicationQueryWrapper = new QueryWrapper<>();
        certApplicationQueryWrapper.eq("is_deleted", 0);
        certApplicationQueryWrapper.eq("approval_process_sign", 1);
        certApplicationQueryWrapper.and(temp -> temp.eq("qualified_type", 105).or().eq("qualified_type", 106));
        List<CertApplication> certApplicationList = certApplicationService.list(certApplicationQueryWrapper);
        for (CertApplication certApplication : certApplicationList) {
            // 存储答案对象
            CertApplicationVo certApplicationVo = new CertApplicationVo();
            BeanUtils.copyProperties(certApplication, certApplicationVo);
            // 通过办证申请表id查询学生id
            Long certApplicationVoId = certApplicationVo.getId();
            // 通过办证申请表id查询办证申请学生信息
            QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
            certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
            List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
            // 通过学生id找到考试合格证明学生表，进而找到考试合格证明表
            Long studentId = certApplicationStudentList.get(0).getStudentId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("student_id", studentId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            Long examPassId = examPassStudents.get(0).getExamPassId();
            // 通过考试合格证明表id找到考试合格证明表
            ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
            // 得到考试期次
            certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());

            // 存入最终答案中
            certApplicationVoList.add(certApplicationVo);
        }

        return certApplicationVoList;
    }

    @Override
    public int fuzerenOneApprovalSubmit(CertApplication certCertificate) {
        certCertificate.setUpdateTime(DateUtils.getNowDate());
        certCertificate.setUpdateId(SecurityUtils.getUserId());
        certCertificate.setAuditId(SecurityUtils.getUserId());
            certCertificate.setApprovalProcessSign(2L);
        return certApplicationService.updateCertApplication(certCertificate);
    }

    @Override
    public List<CertApplicationVo> selectfuzerenCertApplicationTwoList(CertApplication certApplication1) {
        // 返回最终答案集合
        List<CertApplicationVo> certApplicationVoList = new ArrayList<>();
        QueryWrapper<CertApplication> certApplicationQueryWrapper = new QueryWrapper<>();
        certApplicationQueryWrapper.eq("is_deleted", 0);
        certApplicationQueryWrapper.eq("approval_process_sign", 2);
        certApplicationQueryWrapper.and(temp -> temp.eq("qualified_type", 105).or().eq("qualified_type", 106));
        List<CertApplication> certApplicationList = certApplicationService.list(certApplicationQueryWrapper);
        for (CertApplication certApplication : certApplicationList) {
            // 存储答案对象
            CertApplicationVo certApplicationVo = new CertApplicationVo();
            BeanUtils.copyProperties(certApplication, certApplicationVo);
            // 通过办证申请表id查询学生id
            Long certApplicationVoId = certApplicationVo.getId();
            // 通过办证申请表id查询办证申请学生信息
            QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
            certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
            List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
            // 通过学生id找到考试合格证明学生表，进而找到考试合格证明表
            Long studentId = certApplicationStudentList.get(0).getStudentId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("student_id", studentId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            Long examPassId = examPassStudents.get(0).getExamPassId();
            // 通过考试合格证明表id找到考试合格证明表
            ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
            // 得到考试期次
            certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());

            // 存入最终答案中
            certApplicationVoList.add(certApplicationVo);
        }

        return certApplicationVoList;
    }

    @Override
    public int fuzerenTwoApprovalSubmit(CertApplication certCertificate) {
        certCertificate.setUpdateTime(DateUtils.getNowDate());
        certCertificate.setUpdateId(SecurityUtils.getUserId());
        certCertificate.setApprovalId(SecurityUtils.getUserId());
            certCertificate.setApprovalProcessSign(3L);
        return certApplicationService.updateCertApplication(certCertificate);
    }

    @Override
    public int fuzerengaizhangCertApplication(CertApplication certApplication) {
        certApplication.setGaizhangId(SecurityUtils.getUserId());
        // 通过办证申请表信息得到办证申请表需要生产的pdf
        certApplicationService.updateCertApplication(certApplication);
        CertApplicationVo certApplicationVo = getCertInformationByCertInformation(certApplication.getId());
        // 通过办证申请表id得到学生信息
        List<CertApplicationStudentVo> certApplicationStudentVoList = getCertStudentInformationByCertApplicationId(certApplication.getId());
        certApplicationVo.setCertApplicationStudentVoList(certApplicationStudentVoList);
        // 生产办证申请表pdf
        String pdfUrl = getCertPdfByCertInformation(certApplicationVo);
        ExamApplicationServiceImpl applicationService = new ExamApplicationServiceImpl();
        String pdfVirtualUrlByTrueUrl = applicationService.getPdfVirtualUrlByTrueUrl(pdfUrl);
        certApplication.setCertApplicationPdf(pdfVirtualUrlByTrueUrl);
        certApplication.setUpdateId(SecurityUtils.getUserId());
        certApplication.setUpdateTime(DateUtils.getNowDate());
        certApplication.setIsPass(1);
        certApplication.setApprovalProcessSign(4L);

        return certApplicationService.updateCertApplication(certApplication);
    }

    @Override
    public List<CertApplicationVo> selectfuzerenCertApplicationgaizhangList(CertApplication certApplication1) {
        // 返回最终答案集合
        List<CertApplicationVo> certApplicationVoList = new ArrayList<>();
        QueryWrapper<CertApplication> certApplicationQueryWrapper = new QueryWrapper<>();
        certApplicationQueryWrapper.eq("is_deleted", 0);
        certApplicationQueryWrapper.eq("approval_process_sign", 3);
        certApplicationQueryWrapper.and(temp -> temp.eq("qualified_type", 105).or().eq("qualified_type", 106));
        List<CertApplication> certApplicationList = certApplicationService.list(certApplicationQueryWrapper);
        for (CertApplication certApplication : certApplicationList) {
            // 存储答案对象
            CertApplicationVo certApplicationVo = new CertApplicationVo();
            BeanUtils.copyProperties(certApplication, certApplicationVo);
            // 通过办证申请表id查询学生id
            Long certApplicationVoId = certApplicationVo.getId();
            // 通过办证申请表id查询办证申请学生信息
            QueryWrapper<CertApplicationStudent> certApplicationStudentQueryWrapper = new QueryWrapper<>();
            certApplicationStudentQueryWrapper.eq("cert_application_id", certApplicationVoId);
            List<CertApplicationStudent> certApplicationStudentList = certApplicationStudentService.list(certApplicationStudentQueryWrapper);
            // 通过学生id找到考试合格证明学生表，进而找到考试合格证明表
            Long studentId = certApplicationStudentList.get(0).getStudentId();
            QueryWrapper<ExamPassStudent> examPassStudentQueryWrapper = new QueryWrapper<>();
            examPassStudentQueryWrapper.eq("student_id", studentId);
            List<ExamPassStudent> examPassStudents = examPassStudentService.list(examPassStudentQueryWrapper);
            Long examPassId = examPassStudents.get(0).getExamPassId();
            // 通过考试合格证明表id找到考试合格证明表
            ExamPassCertificate examPassCertificateServiceById = examPassCertificateService.getById(examPassId);
            // 得到考试期次
            certApplicationVo.setClassTerm(examPassCertificateServiceById.getClassTerm());

            // 存入最终答案中
            certApplicationVoList.add(certApplicationVo);
        }

        return certApplicationVoList;
    }
}
